package com.wythe.mall.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.wythe.mall.R;
import com.wythe.mall.adapter.FragmentTabAdapter;
import com.wythe.mall.base.BaseActivity;
import com.wythe.mall.model.StoreBean;
import com.wythe.mall.permissions.PermissionsActivity;
import com.wythe.mall.permissions.PermissionsChecker;
import com.wythe.mall.ui.CartFragment;
import com.wythe.mall.ui.CategoryFragment;
import com.wythe.mall.ui.HomeFragment;
import com.wythe.mall.ui.PersonalFragment;
import com.wythe.mall.utils.GPSUtil;
import com.wythe.mall.utils.GotoActivity;
import com.wythe.mall.utils.SharePreferenceUtils;
import com.wythe.mall.utils.UserManager;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends BaseActivity implements View.OnClickListener {
	public static final String TAG = MainActivity.class.getSimpleName();
	private RadioGroup radio_button_group;
	private RadioButton navigation_tab_home,navigation_tab_category,navigation_tab_cart,navigation_tab_personal;
	private FragmentTabAdapter tabAdapter;
	public List<Fragment> fragments = new ArrayList<Fragment>();

	private int mLabel = 0;
	private StoreBean mBean;
	private int mCurrentPosition = 0;



	public int getmLabel() {
		return mLabel;
	}

	public void setmLabel(int mLabel) {
		this.mLabel = mLabel;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Fresco.initialize(getApplicationContext());
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		mBean = (StoreBean) SharePreferenceUtils.readObject(this, "store");
		if (mBean == null) {
			GotoActivity.gotoActiviy(this,DeliveryStoreActivity.class);
		}
	}

	protected void initView() {
		radio_button_group = (RadioGroup) findViewById(R.id.radio_button_group);

		navigation_tab_home = (RadioButton) findViewById(R.id.navigation_tab_home);
		navigation_tab_category = (RadioButton) findViewById(R.id.navigation_tab_category);
		navigation_tab_cart = (RadioButton) findViewById(R.id.navigation_tab_cart);
		navigation_tab_personal = (RadioButton) findViewById(R.id.navigation_tab_personal);

		navigation_tab_home.setOnClickListener(this);
		navigation_tab_category.setOnClickListener(this);
		navigation_tab_cart.setOnClickListener(this);
		navigation_tab_personal.setOnClickListener(this);

		fragments.add(new HomeFragment());
		fragments.add(new CategoryFragment());
//		fragments.add(new TabCFm());
		fragments.add(new CartFragment());
		fragments.add(new PersonalFragment());
		tabAdapter = new FragmentTabAdapter(this, fragments, R.id.fragment_content, radio_button_group);
	}

	public void navigateTo(int index){

		((RadioButton)radio_button_group.getChildAt(index)).setChecked(true);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

	}

	@Override
	public void onDestroy() {
		super.onDestroy();

	}

	@Override
	public void onStart() {
		super.onStart();

	}
	@Override
	protected void onPause() {
		super.onPause();

	}

	@Override
	protected void onResume() {
		super.onResume();
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			if (bundle.getInt("skip") == 111) {
				navigateTo(2);
			}
		}
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.navigation_tab_home:
				mCurrentPosition = 0;
				break;
			case R.id.navigation_tab_category:
				mCurrentPosition = 1;
				break;
			case R.id.navigation_tab_cart:
				if (UserManager.getInstance().isLogin()) {
					mCurrentPosition = 2;
				} else {
					((RadioButton)radio_button_group.getChildAt(mCurrentPosition)).setChecked(true);
				}
				break;
			case R.id.navigation_tab_personal:
				if (UserManager.getInstance().isLogin()) {
					mCurrentPosition = 3;
				} else {
					((RadioButton)radio_button_group.getChildAt(mCurrentPosition)).setChecked(true);
				}

				break;
		}
	}


}
